﻿Namespace Errors
    Public Class ToolKitUnexpectedError
        Private Shared support As New Data.SupportTableAdapters.ErrorsTableAdapter
        Public Shared Sub Report(ByVal ex As Exception, ByVal ClassName As String, ByVal MethodName As String)
            If ex Is Nothing Then
                HttpContext.Current.Response.Clear()
                HttpContext.Current.Response.ContentType = "text/plain"
                HttpContext.Current.Response.Write("Untraceable Error")
                HttpContext.Current.Response.Flush()
                HttpContext.Current.Response.End()
            End If
            Try
                support.Insert(ClassName, MethodName, ex.ToString, GetException(ex))
            Catch iex As Exception

            End Try
            Try
                HttpContext.Current.Response.Clear()
                HttpContext.Current.Response.ContentType = "text/plain"
                HttpContext.Current.Response.Write(GetException(ex))
                HttpContext.Current.Response.Flush()
                HttpContext.Current.Response.End()
            Catch iex As Exception
                HttpContext.Current.Response.Clear()
                HttpContext.Current.Response.ContentType = "text/plain"
                HttpContext.Current.Response.Write("Level 3 Error:" + ex.Message)
                HttpContext.Current.Response.Flush()
                HttpContext.Current.Response.End()
            End Try
        End Sub
        Public Shared Function GetException(ByVal ex As Exception) As String
            If ex Is Nothing Then
                Return ""
            Else
                Dim Result As String = ""
                If Not TypeOf ex Is System.Web.HttpUnhandledException Then
                    Result = "[" & ex.Message & vbCrLf
                    Result &= "Source: " & ex.Source & vbCrLf
                    Result &= "StackTrace: " & ex.StackTrace & vbCrLf
                    Result &= "]" & vbCrLf & vbCrLf & vbCrLf
                End If
                If ex.InnerException IsNot Nothing Then
                    Result &= GetException(ex.InnerException)
                End If
                Return Result
            End If
        End Function
    End Class
End Namespace

